<?php
namespace app\index\controller;
use think\Controller;
use think\Validate;
use think\Db;
use think\Session;
use think\paginate;
use think\captcha;
use think\helper;
use think\Loader;


class Admin extends Controller
{
    public function _initialize()
	{
        if(Session::has('username')==false)
        {
            return view('/login');
        }
    }
    public function index()
    {
        if(Session::has('live_username')==false){
            return view('/login');
        }
        return $this->fetch('/admin_index');
    }
    public function login()
    {
       return view('/login');
    }
    public function dologin()
    {
        $username=$_POST['username'];
        $pwd=md5($_POST['password']);
        $captcha=$_POST['code'];   

        if(!captcha_check($captcha)){
            $this->error('验证码不正确');
        }else{
            $data=['username'=>$username,'password'=>$pwd];
            $id=Db::name('user')->where($data)->find();
            if($id)
            {
                Session::set('live_username',"$username");
                $this->redirect('Admin/index');
            }else{
                $this->error('账号或密码不正确！');
            }
        }
    }
    public function loginout()
    {
        Session::delete('live_username');
        return view('/login');
    }
    public function admin_category()
    {
        return $this->fetch('/admin_category');
    }
    
    public function welcome()
    {
    	return $this->fetch('/welcome');
    }




    //-------------------------封面大图----------------------------------
    public function admin_bigpic()
    {
        $list=Db::name('image_big')->select();
        $this->assign('list',$list);
        $count=Db::name('image_big')->count();
        $this->assign('count',$count);
        return $this->fetch('/admin_big_pic');
    }
    public function admin_big_pic_add()
    {
        return $this->fetch('/admin_big_pic_add');
    }
    public function admin_big_pic_edit($id)
    {
        $id=$_GET['id'];
        $img=Db::name('image_big')->where('id',$id)->find();
        $this->assign('img',$img); 
        return $this->fetch('/admin_big_pic_edit');
    }
    public function admin_big_pic_doadd()
    {
        $file = request()->file('image');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $url=$info->getSaveName();
                $url=explode('\\',$url);
                $url=implode('/',$url);
            }else{
                echo $file->getError();
            }
        }
        $title=$_POST['title'];
        $content=$_POST['content'];

        $data=[
            'image_url'=>$url,
            'image_title'=>$title,
            'image_content'=>$content
        ];
        $id=Db::table('image_big')->insertGetId($data);
        if($id){
            return $this->admin_bigpic();
        }else{
            $this->error('添加失败！请重新添加');
        }
    }
    public function admin_big_pic_doedit()
    {
        $id=$_POST['id'];
        $img=Db::name('image_big')->where('id',$id)->find();
        $url=$img['image_url'];

        $file = request()->file('image');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $url=$info->getSaveName();
                $url=explode('\\',$url);
                $url=implode('/',$url);
            }else{
                echo $file->getError();
            }
        }
        
        $title=$_POST['title'];
        $content=$_POST['content'];
        $res=Db::table('image_big')->where('id', $id)->update([
            'image_url'=>$url,
            'image_title'=>$title,
            'image_content'=>$content
        ]);
        if($res){
            return $this->admin_bigpic();
        }else{
            $this->error('添加失败！请重新添加');
        }
    }
    public function admin_big_pic_del($id)
    {
        Db::table('image_big')->where('id',$id)->delete();
        return $this->admin_bigpic();
    }




    //-------------------------公告栏信息----------------------------------
    public function admin_report()
    {
        $list=Db::name('report')->select();
        $this->assign('list',$list);
        $count=Db::name('report')->count();
        $this->assign('count',$count);
        return $this->fetch('/admin_report');
    }
    public function admin_report_add()
    {
        $report=$_POST['report'];

        $data=[
            'report'=>$report,
            'report_time'=>time()
        ];
        $id=Db::table('report')->insertGetId($data);
        if($id){
            return $this->admin_report();
        }else{
            $this->error('添加失败！请重新添加');
        }
    }
    public function admin_report_del($id)
    {
        Db::table('report')->where('id',$id)->delete();
        return $this->admin_report();
    }





    //-------------------------首页博客简略信息----------------------------------
    public function admin_blog()
    {
        $list=Db::name('blog')->select();
        $this->assign('list',$list);
        $count=Db::name('blog')->count();
        $this->assign('count',$count);
        return $this->fetch('/admin_blog');
    }
    public function admin_blog_add()
    {
        return view('/admin_blog_add');
    }
    public function admin_blog_doadd()
    {
        if(empty(request()->file('image')) || empty($_POST['remark']) ||empty($_POST['title']) || empty($_POST['author']) || empty($_POST['category']) || empty($_POST['editorValue'])){
            $this->error('要全部填写哦~~');
        }
        $file = request()->file('image');
        if($file){
            $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
            if($info){
                $url=$info->getSaveName();
                $url=explode('\\',$url);
                $url=implode('/',$url);
            }else{
                echo $file->getError();
            }
        }
        $remark=$_POST['remark'];
        $image=$url;
        $title=$_POST['title'];
        $author=$_POST['author'];
        $category=$_POST['category'];
        $category=implode(',',$category);
        $content=$_POST['editorValue'];

        $data=[
            'title'=>$title,
            'content'=>$content,
            'image'=>$image,
            'author'=>$author,
            'category'=>$category,
            'create_time'=>time(),
            'edit_time'=>time(),
            'remark'=>$remark
        ];
        $id=Db::table('blog')->insertGetId($data);
        if($id){
            return $this->admin_blog();
        }else{
            $this->error('添加失败！请重新添加');
        }
    }



    
}



?>